package com.hspedu.newData.dmsxl.回溯.分割;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-26 
 * @Version: 1.0
 */

// todo 需要剪枝优化  不知道怎么写，还需要学习一下
public class LC131分割回文串 {

    List<List<String>> res = new ArrayList<>();
    LinkedList<String> path = new LinkedList<>();

    public List<List<String>> partition(String s) {
        backtracking(s,0,new StringBuilder());
        return res;
    }

    public void backtracking(String s,int startIndex,StringBuilder sb){
        if (startIndex == s.length()){
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = startIndex; i < s.length(); i++) {
            sb.append(s.charAt(i));
            if (isPalindrome(sb)){
                path.add(sb.toString());
                backtracking(s,i + 1,new StringBuilder());
                path.removeLast();
            }
        }

    }

    public boolean isPalindrome(StringBuilder s){
        for (int i = 0, j = s.length() - 1; i < j; i++,j--) {
            if (s.charAt(i) != s.charAt(j)){
                return false;
            }
        }
        return true;
    }

    @Test
    public void test() {
        // 测试代码

        List<List<String>> aab = partition("aab");
        System.out.println("======");
    }
}
